//
//  CustomTableViewCell.m
//  QQchat
//
//  Created by cocoazxj@163.com on 15/3/29.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "CustomTableViewCell.h"
#define EDGE_INSET 20

@interface CustomTableViewCell ()
@property (nonatomic, strong) IBOutlet UILabel *time;
@property (nonatomic, strong) IBOutlet UIButton *text;
@property (nonatomic, strong) IBOutlet UIImageView *icon;
@end

@implementation CustomTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UILabel *time = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
        [time setTextAlignment:NSTextAlignmentCenter];
        [time setFont:[UIFont systemFontOfSize:14]];
        self.time = time;
        [self.contentView addSubview:time];
        
        UIButton *text = [UIButton buttonWithType:UIButtonTypeCustom];
        [text.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [text.titleLabel setNumberOfLines:0];
        [text setBackgroundColor:[UIColor redColor]];
        [text setContentEdgeInsets:UIEdgeInsetsMake(EDGE_INSET, EDGE_INSET, EDGE_INSET, EDGE_INSET)];
        self.text = text;
        [self.contentView addSubview:text];
        
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(240, 40, 40, 40)];
        self.icon = icon;
        [self.contentView addSubview:icon];
        self.backgroundColor = [UIColor clearColor];
        
        // contentView背景颜色为透明
//        self.contentView.backgroundColor = [UIColor yellowColor];
        
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setChatmodel:(ChatModel *)chatmodel
{
    if (chatmodel.isTime)
    {
        NSLog(@"hidden");
        [self.time setHidden:YES];
    }
    else
    {
        self.time.text = chatmodel.time;
    }
    
    NSLog(@"==%d",chatmodel.type);
    if(chatmodel.type)
    {
        [self.icon setFrame:CGRectMake(260, 40, 50, 50)];
        [self.icon setImage:[UIImage imageNamed:@"me"]];
        NSLog(@"-=======%f",320 - chatmodel.textW - 70);
        [self.text setFrame:CGRectMake(320 - 70 - chatmodel.textW - 40, 40, chatmodel.textW + 40, chatmodel.textH)];
        [self setBackGroundImage:@"chat_send_nor"];
    }
    else
    {
        [self.icon setFrame:CGRectMake(10, 40, 50, 50)];
        [self.icon setImage:[UIImage imageNamed:@"other"]];
        [self.text setFrame:CGRectMake(70, 40, chatmodel.textW + 40, chatmodel.textH)];
        [self.text setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setBackGroundImage:@"chat_recive_nor"];
        
    }
    [self.text setTitle:chatmodel.text forState:UIControlStateNormal];
}

- (void)setBackGroundImage:(NSString *)image
{
    UIImage *bg = [UIImage imageNamed:image];
    CGFloat w = bg.size.width*0.5;
    CGFloat h = bg.size.height*0.5;
    UIImage *newBg = [bg resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w) resizingMode:UIImageResizingModeStretch];
    [self.text setBackgroundImage:newBg forState:UIControlStateNormal];
}
@end
